//
//  HomePageController.m
//  Doc Bao
//
//  Created by Hai Nguyen on 9/16/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "HomePageController.h"
#import "Post.h"
#import "PostExtensions.h"
#import "News.h"
#import "NewsExtensions.h"
#import "HomeCell.h"

@implementation HomePageController
@synthesize table;
@synthesize postList;
@synthesize newsList;
@synthesize cell;
@synthesize headerSectionView;
@synthesize newsFavicon;
@synthesize newsName;
@synthesize vnExpressPost;
@synthesize tuoitrePost;
@synthesize vietnamnetPost;
@synthesize dantriPost;
@synthesize baodatvietPost;
@synthesize thanhnienPost;
@synthesize nldPost;

- (void) loadHomePage{
}

- (void) viewDidLoad {
	[super viewDidLoad];
	NSLog(@"viewDidLoad");
	postList = [NSMutableArray arrayWithArray:[Post getPosts]];
	NSLog(@"Post :%@", postList);
	newsList = [NSMutableArray arrayWithArray:[News getNews]];
	for (News *news in newsList) {
		NSLog(@"%@", news.Name);
	}
	NSLog(@"News: %@", newsList);
	vnExpressPost = [[NSMutableArray alloc] initWithArray:[Post getPostsWithNews:1]];
	tuoitrePost = [[NSMutableArray alloc] initWithArray:[Post getPostsWithNews:2]];
	vietnamnetPost = [[NSMutableArray alloc] initWithArray:[Post getPostsWithNews:3]];
	dantriPost = [[NSMutableArray alloc] initWithArray:[Post getPostsWithNews:4]];
	baodatvietPost = [[NSMutableArray alloc] initWithArray:[Post getPostsWithNews:5]];
	thanhnienPost = [[NSMutableArray alloc] initWithArray:[Post getPostsWithNews:6]];
	nldPost = [[NSMutableArray alloc] initWithArray:[Post getPostsWithNews:7]];	
	[table reloadData];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
    [super viewDidUnload];
	self.table = nil;
}
- (void)dealloc {
	[super dealloc];
	self.table = nil;
	self.newsList = nil;
	self.cell = nil;
	self.postList = nil;
	self.headerSectionView = nil;
	self.newsFavicon = nil;
	self.newsName = nil;
	self.vnExpressPost = nil;
	self.tuoitrePost = nil;
}

#pragma mark -
#pragma mark Table View Data Source Method

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
	return [newsList count];
}

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section{
	return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
	static NSString *CellIdentifier = @"Cell";    
    cell = (HomeCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"HomeCell" owner:self options:nil];
    }
	switch (indexPath.section) {
		case 0:{
			Post *post = [vnExpressPost objectAtIndex:[indexPath row]];
			UIImage *image = [[UIImage alloc] initWithData:post.Thumbnail];
			cell.thumbnail.image = image;
			cell.title.text = post.Title;
			cell.description.text = post.Description;
		}
			break;
		case 1:{
			Post *post = [tuoitrePost objectAtIndex:[indexPath row]];
			UIImage *image = [[UIImage alloc] initWithData:post.Thumbnail];
			cell.thumbnail.image = image;
			cell.title.text = post.Title;
			cell.description.text = post.Description;
		}
			break;
		case 2:{
			Post *post = [vietnamnetPost objectAtIndex:[indexPath row]];
			UIImage *image = [[UIImage alloc] initWithData:post.Thumbnail];
			cell.thumbnail.image = image;
			cell.title.text = post.Title;
			cell.description.text = post.Description;
		}
			break;
		case 3:{
			Post *post = [dantriPost objectAtIndex:[indexPath row]];
			UIImage *image = [[UIImage alloc] initWithData:post.Thumbnail];
			cell.thumbnail.image = image;
			cell.title.text = post.Title;
			cell.description.text = post.Description;
			
		}
			break;
		case 4:	{
			Post *post = [baodatvietPost objectAtIndex:[indexPath row]];
			UIImage *image = [[UIImage alloc] initWithData:post.Thumbnail];
			cell.thumbnail.image = image;
			cell.title.text = post.Title;
			cell.description.text = post.Description;
			
		}
			break;
		case 5:{
			Post *post = [thanhnienPost objectAtIndex:[indexPath row]];
			UIImage *image = [[UIImage alloc] initWithData:post.Thumbnail];
			cell.thumbnail.image = image;
			cell.title.text = post.Title;
			cell.description.text = post.Description;
			
		}
			break;
		case 6:{
			Post *post = [nldPost objectAtIndex:[indexPath row]];
			UIImage *image = [[UIImage alloc] initWithData:post.Thumbnail];
			cell.thumbnail.image = image;
			cell.title.text = post.Title;
			cell.description.text = post.Description;			
		}
			break;
		default:
			break;
	}
	return cell;
}

/*- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
	NSString *sectionName = [[NSString alloc] init];
	switch (section) {
		case 0:
			sectionName = @"VNExpress";
			break;
		case 1:
			sectionName = @"Tuổi Trẻ Online";
			break;
		case 2:
			sectionName = @"VietNamNet";
			break;
		case 3:
			sectionName = @"Dân Trí";
			break;
		case 4:
			sectionName = @"Báo Đất Việt";
			break;
		case 5:
			sectionName = @"Thanh Niên  Online";
			break;
		case 6:
			sectionName = @"Người lao động";
			break;
	
		default:
			break;
	}
	return sectionName;
	[sectionName release];
}*/

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{	
	NSArray *array =[NSArray arrayWithArray:[News getNews]];
	
	switch (section) {
		case 0:{
			News *news = [array objectAtIndex:5];
			newsFavicon.image = [UIImage imageNamed:news.Logo];
			newsName.text = news.Name;			
		}
			break;
		case 1:{
			News *news = [array objectAtIndex:6];
			newsFavicon.image = [UIImage imageNamed:news.Logo];
			newsName.text = news.Name;			
		}
			break;
		case 2:{
			News *news = [array objectAtIndex:1];
			newsFavicon.image = [UIImage imageNamed:news.Logo];
			newsName.text = news.Name;			
		}
			break;
		case 3:{
			News *news = [array objectAtIndex:3];
			newsFavicon.image = [UIImage imageNamed:news.Logo];
			newsName.text = news.Name;			
		}
			break;
		case 4:{
			News *news = [array objectAtIndex:0];
			newsFavicon.image = [UIImage imageNamed:news.Logo];
			newsName.text = news.Name;			
		}
			break;
		case 5:{
			News *news = [array objectAtIndex:2];
			newsFavicon.image = [UIImage imageNamed:news.Logo];
			newsName.text = news.Name;			
		}
			break;
		case 6:{
			News *news = [array objectAtIndex:4];
			newsFavicon.image = [UIImage imageNamed:news.Logo];
			newsName.text = news.Name;			
		}
		break;	}
	return headerSectionView;
}

-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
	return 44;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
	return 75;
}

@end
